/**
 * Title:        选择框子字段检测
 * Company:      泛微软件
 */


package checkWithCondit;

import general.BaseBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import weaver.general.Util;



public class ChildSelectItemCheck extends BaseBean implements checkWithConBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("ChildSelectItemCheck  start");

        boolean isoracle = (recordSet.getDBType()).equals("oracle") ;
        
        List resultList = new ArrayList();
        String result = "";
        if(Util.getIntValue((String)dataMap.get("workflowid")) <= 0){
            result = resultERROR;
            resultList.add("请选择流程路径");
            return formatReturnJsonCheck(result, resultList,this);
        }
        String workflowid = Util.null2String(dataMap.get("workflowid"));
        String strsql = "";
        strsql+=" select billlabel.labelname as billname,fieldabel.labelname as fieldname,childlabel.labelname as childname,workflow_billfield.id as fieldid,workflow_billfield.childfieldid ";
        strsql+="   from workflow_billfield ";
        strsql+="   inner join  workflow_billfield childfield ";
        strsql+="   on childfield.id = workflow_billfield.childfieldid ";
        strsql+="   inner join  workflow_bill ";
        strsql+="   on workflow_billfield.billid = workflow_bill.id ";
        strsql+="   inner join  workflow_base ";
        strsql+="   on workflow_base.formid = workflow_bill.id ";
        strsql+="      and workflow_base.id = " + workflowid;
        strsql+="   inner join HtmlLabelInfo billlabel ";
        strsql+="   on billlabel.languageid = 7 and billlabel.indexid = workflow_bill.namelabel ";
        strsql+="   inner join HtmlLabelInfo fieldabel ";
        strsql+="   on fieldabel.languageid = 7 and fieldabel.indexid = workflow_billfield.fieldlabel ";
        strsql+="   inner join HtmlLabelInfo childlabel ";
        strsql+="   on childlabel.languageid = 7 and childlabel.indexid = childfield.fieldlabel ";
        strsql+="  where workflow_billfield.childfieldid in (select childfieldid ";
        strsql+="                           from (select count(1) as cnt, childfieldid, billid ";
        strsql+="                                   from workflow_billfield ";
        strsql+="                                  where fieldhtmltype = 5 ";
        strsql+="                                    and childfieldid > 0 ";
        strsql+="                                    and billid <0 ";
        strsql+="                                  group by childfieldid, billid) aaa ";
        strsql+="                          where aaa.cnt > 1) ";
        strsql+="  order by workflow_billfield.billid,workflow_billfield.childfieldid ";
        recordSet.executeSql(strsql);
        int cnt = 0;
        while(recordSet.next()){
            result = resultNeedFix;
            Map resultMap = new HashMap<String,String>();
            resultMap.put("showMessage","[" + recordSet.getString("billname") + "]表单中字段[" + recordSet.getString("fieldname") + "]有重复使用的子选择框[" + recordSet.getString("childname") + "]，请修复。");
            resultMap.put("billfieldid",recordSet.getString("fieldid"));
            resultMap.put("buttonname","解除关联");
            resultList.add(resultMap);
            cnt++;
        }
        if(cnt <=0){
            result = resultOk;
            resultList.add("选择框子字段数据正常!");
        }
        (new BaseBean()).writeLog("ChildSelectItemCheck  end");
        return formatReturnJsonCheck(result, resultList,this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {

        setRecordSet();
        List resultList = new ArrayList();
        String result = "";
        if(dataMap != null && dataMap.get("billfieldid") != null){
            //根据参数获取列表
            String billfieldids = (String)dataMap.get("billfieldid");
            String divids =  (String)dataMap.get("divid");
            List<String> billfieldidList = Util.splitString2List(billfieldids, ",");
            List<String> dividList = Util.splitString2List(divids, ",");
            for(int i = 0; i < billfieldidList.size(); i++){
                String billfieldid = billfieldidList.get(i);
                String divid = dividList.get(i);
                String strSql = " update workflow_billfield set childfieldid = null where id = " + billfieldid;
                recordSet.executeUpdate(strSql);
                recordSet.executeSql("select childfieldid from workflow_billfield where id = " + billfieldid);
                if(recordSet.next()){
                    Map resultMap = new HashMap<String,String>();
                    resultMap.put("result",resultOk);
                    resultMap.put("divid",divid);
                    resultMap.put("fixMessage","解除成功");
                    resultList.add(resultMap);
                }
            }
            result = resultOk;
            return formatReturnJsonFix(result, resultList,this);
        }
        needReload = "1";
        return checkData(dataMap);
        
    }

    @Override
    public String getClassName() {
        return "选择框子字段检测";
    }

    @Override
    public String getClassDesc() {
        return "选择框子字段检测（是否有关联，是否重复关联）";
    }

    @Override
    public String getPopupUrl() {
        return "/flowtoolbar/checkWithCondit/WorkflowCond.jsp";
    }

}



